package com.sedu.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sedu.common.annotation.Log;
import com.sedu.common.core.controller.BaseController;
import com.sedu.common.core.domain.AjaxResult;
import com.sedu.common.enums.BusinessType;
import com.sedu.manage.domain.AttentionStudent;
import com.sedu.manage.service.IAttentionStudentService;
import com.sedu.common.utils.poi.ExcelUtil;
import com.sedu.common.core.page.TableDataInfo;

/**
 * 考勤重点关注Controller
 *
 * @author yulinfeng
 * @date 2025-03-29
 */
@RestController
@RequestMapping("/manage/attention_student")
public class AttentionStudentController extends BaseController {
    @Autowired
    private IAttentionStudentService attentionStudentService;

    /**
     * 查询考勤重点关注列表
     */
    @PreAuthorize("@ss.hasPermi('manage:attention_student:list')")
    @GetMapping("/list")
    public TableDataInfo list(AttentionStudent attentionStudent) {
        startPage();
        List<AttentionStudent> list = attentionStudentService.selectAttentionStudentList(attentionStudent);
        return getDataTable(list);
    }

    /**
     * 导出考勤重点关注列表
     */
    @PreAuthorize("@ss.hasPermi('manage:attention_student:export')")
    @Log(title = "考勤重点关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttentionStudent attentionStudent) {
        List<AttentionStudent> list = attentionStudentService.selectAttentionStudentList(attentionStudent);
        ExcelUtil<AttentionStudent> util = new ExcelUtil<AttentionStudent>(AttentionStudent.class);
        util.exportExcel(response, list, "考勤重点关注数据");
    }

    /**
     * 获取考勤重点关注详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:attention_student:query')")
    @GetMapping(value = "/{attentionId}")
    public AjaxResult getInfo(@PathVariable("attentionId") Long attentionId) {
        return success(attentionStudentService.selectAttentionStudentByAttentionId(attentionId));
    }

    /**
     * 新增考勤重点关注
     */
    @PreAuthorize("@ss.hasPermi('manage:attention_student:add')")
    @Log(title = "考勤重点关注", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttentionStudent attentionStudent) {
        return toAjax(attentionStudentService.insertAttentionStudent(attentionStudent));
    }

    /**
     * 修改考勤重点关注
     */
    @PreAuthorize("@ss.hasPermi('manage:attention_student:edit')")
    @Log(title = "考勤重点关注", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttentionStudent attentionStudent) {
        // 如果attentionId为null,但是存在studentId,则说明是修改
        if (attentionStudent.getAttentionId() == null && attentionStudent.getStudentId() != null) {
            // 新建AttentionStudent
            AttentionStudent attentionStudentId = new AttentionStudent();
            attentionStudentId.setStudentId(attentionStudent.getStudentId());
            AttentionStudent attentionStudentTemp = attentionStudentService.selectAttentionStudentList(attentionStudentId).get(0);
            // 检查查询结果是否为 null
            if (attentionStudentTemp == null) {
                // 如果查询结果为 null，则新增记录
                return toAjax(attentionStudentService.insertAttentionStudent(attentionStudent));
                // return AjaxResult.error("未找到与该学生ID对应的考勤重点关注记录");
            } else {
                attentionStudent.setAttentionId(attentionStudentTemp.getAttentionId());
                return toAjax(attentionStudentService.updateAttentionStudent(attentionStudent));
            }
        }
        return toAjax(attentionStudentService.updateAttentionStudent(attentionStudent));
    }

    /**
     * 删除考勤重点关注
     */
    @PreAuthorize("@ss.hasPermi('manage:attention_student:remove')")
    @Log(title = "考勤重点关注", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attentionIds}")
    public AjaxResult remove(@PathVariable Long[] attentionIds) {
        return toAjax(attentionStudentService.deleteAttentionStudentByAttentionIds(attentionIds));
    }
}
